﻿package com.rbac.system.controller;

import com.rbac.system.common.Result;
import com.rbac.system.service.EmailVerificationService;
import com.rbac.system.service.SystemConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 邮箱验证控制器
 */
@RestController
@RequestMapping("/api/email")
@CrossOrigin
public class EmailVerificationController {
    
    @Autowired
    private EmailVerificationService emailVerificationService;

    @Autowired
    private SystemConfigService systemConfigService;
    
    /**
     * 发送注册验证码
     */
    @PostMapping("/send-register-code")
    public Result<Void> sendRegisterCode(@RequestBody Map<String, String> request) {
        String email = request.get("email");
        if (email == null || email.trim().isEmpty()) {
            return Result.error("邮箱地址不能为空");
        }
        return emailVerificationService.sendRegisterCode(email.trim());
    }
    
    /**
     * 发送登录验证码
     */
    @PostMapping("/send-login-code")
    public Result<Void> sendLoginCode(@RequestBody Map<String, String> request) {
        String email = request.get("email");
        if (email == null || email.trim().isEmpty()) {
            return Result.error("邮箱地址不能为空");
        }
        return emailVerificationService.sendLoginCode(email.trim());
    }
    
    /**
     * 验证注册验证码
     */
    @PostMapping("/verify-register-code")
    public Result<Void> verifyRegisterCode(@RequestBody Map<String, String> request) {
        String email = request.get("email");
        String code = request.get("code");
        
        if (email == null || email.trim().isEmpty()) {
            return Result.error("邮箱地址不能为空");
        }
        if (code == null || code.trim().isEmpty()) {
            return Result.error("验证码不能为空");
        }
        
        return emailVerificationService.verifyRegisterCode(email.trim(), code.trim());
    }
    
    /**
     * 验证登录验证码
     */
    @PostMapping("/verify-login-code")
    public Result<Void> verifyLoginCode(@RequestBody Map<String, String> request) {
        String email = request.get("email");
        String code = request.get("code");
        
        if (email == null || email.trim().isEmpty()) {
            return Result.error("邮箱地址不能为空");
        }
        if (code == null || code.trim().isEmpty()) {
            return Result.error("验证码不能为空");
        }
        
        return emailVerificationService.verifyLoginCode(email.trim(), code.trim());
    }
    
    /**
     * 检查邮箱是否已注册
     */
    @GetMapping("/check-email/{email}")
    public Result<Map<String, Boolean>> checkEmail(@PathVariable String email) {
        boolean isRegistered = emailVerificationService.isEmailRegistered(email);
        return Result.success(Map.of("isRegistered", isRegistered));
    }

    /**
     * 获取邮箱功能配置状态（公开API）
     */
    @GetMapping("/config/status")
    public Result<Map<String, Boolean>> getEmailConfigStatus() {
        Map<String, Boolean> status = Map.of(
            "emailEnabled", systemConfigService.isEmailEnabled(),
            "emailRegisterEnabled", systemConfigService.isEmailRegisterEnabled(),
            "emailLoginEnabled", systemConfigService.isEmailLoginEnabled()
        );
        return Result.success(status);
    }
}


